import apis from './apis'

// 时间获取
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


const Request = (url, method, data, header) => { //接收所需要的参数，如果不够还可以自己自定义参数
  var promise = new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      data: data ? data : null,
      method: method,
      header: header ? header : {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //接口调用成功
        resolve(res); //根据业务需要resolve接口返回的json的数据
      },
      fail: function (res) {
        // fail调用接口失败
        reject(res);
      }
    })
  });
  return promise; //注意，这里返回的是promise对象
}

const UploadFile = (url, filePath, formData) => {
  var promise = new Promise(function (resolve, reject) {
    wx.uploadFile({
      url: url,
      filePath: filePath,
      name: 'image',
      formData: formData,
      success: (result) => {
        resolve(result)
      },
      fail: (result) => {
        reject(result)
      },
    });
  })
  return promise;
}

// 请求接口列表
var newOrder_file = (filePath,formData)=>{
  return new Promise((resolve,reject)=>{
    resolve(UploadFile(apis.newOrder,filePath,formData));
  })
}
var newOrder = (data)=>{
  return new Promise((resolve,reject)=>{
    resolve(Request(apis.newOrder,'POST',data))
  })
}
var app_login = (data) => {
  return new Promise((resolve, reject) => {
    resolve(Request(apis.app_login, 'POST', data));
  })
}
var login = (data) => {
  return new Promise((resolve, reject) => {
    resolve(Request(apis.login, 'POST', data));
  })
}
var getphone = (data) => {
  return new Promise((resolve, reject) => {
    resolve(Request(apis.getphone, 'POST', data));
  })
}
var updateBasicInfo = (data) => {
  return new Promise((resolve, reject) => {
    resolve(Request(apis.updateBasicInfo, 'POST', data));
  })
}
var applyRider = (data) => {
  return new Promise((resolve, reject) => {
    resolve(Request(apis.applyRider, 'POST', data));
  })
}
var getUserOrder = (data) => {
  return new Promise((resolve, reject) => {
    resolve(Request(apis.getUserOrder, 'POST', data));
  })
}

var adv = (data) => {
  return new Promise((resolve, reject) => {
    resolve(Request(apis.adv, 'GET', data));
  })
}

var riderConfirmOrder = (data)=>{
  return new Promise((resolve,reject)=>{
    resolve(Request(apis.riderConfirmOrder,'POST',data));
  })
}
var getUnmatchedOrder = (data)=>{
  return new Promise((resolve,reject)=>{
    resolve(Request(apis.getUnmatchedOrder,'POST',data));
  })
}

var pay = (data)=>{
  return new Promise((resolve,reject)=>{
    resolve(Request(apis.pay,'POST',data));
  })
}
var vipRider=(data)=>{
  return new Promise((resolve,reject)=>{
    resolve(Request(apis.vipRider,'POST',data));
  })
}
var getRiderOrder=(data)=>{
  return new Promise((resolve,reject)=>{
    resolve(Request(apis.getRiderOrder,'POST',data));
  })
}
var getRiderStatus=(data)=>{
  return new Promise((resolve,reject)=>{
    resolve(Request(apis.getRiderStatus,'POST',data));
  })
}
var setRiderStatus=(data)=>{
  return new Promise((resolve,reject)=>{
    resolve(Request(apis.setRiderStatus,'POST',data));
  })
}
var refund=(data)=>{
  return new Promise((resolve,reject)=>{
    resolve(Request(apis.refund,'POST',data));
  })
}
export default {
  app_login: app_login,
  login: login,
  getphone: getphone,
  updateBasicInfo: updateBasicInfo,
  formatTime: formatTime,
  applyRider: applyRider,
  getUserOrder: getUserOrder,
  refund: refund,
  adv: adv,
  riderConfirmOrder:riderConfirmOrder,
  getUnmatchedOrder:getUnmatchedOrder,
  pay:pay,
  newOrder_file:newOrder_file,
  newOrder:newOrder,
  vipRider:vipRider,
  getRiderOrder:getRiderOrder,
  getRiderStatus:getRiderStatus,
  setRiderStatus:setRiderStatus,
}